package com.indusfo.spc.service.impl;

import com.indusfo.spc.common.utlis.IOUtils;
import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.UploadErrorException;
import com.indusfo.spc.mapper.ProfileDetailMapper;
import com.indusfo.spc.mapper.ProfileMapper;
import com.indusfo.spc.mapper.UploadMapper;
import com.indusfo.spc.pojo.*;
import com.indusfo.spc.service.ProfileDetailService;
import com.indusfo.spc.untlis.FileUtil;
import com.indusfo.spc.untlis.OfficeTransformThread;
import com.indusfo.spc.untlis.UuidUtil;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author chenm
 * @create 2019-11-27 9:51
 */
@Service
public class ProfileDetailServiceImpl implements ProfileDetailService {



  @Resource
  private ProfileDetailMapper profileDetailMapper;


  @Resource
  private UploadMapper uploadMapper;


  @Override
  public List<ProfileDetail> selectProFileList(ProfileDetail profileDetail) {
    return profileDetailMapper.selectByMatId(profileDetail.getMatId());
  }

  @Override
  public String checkNoRepeatExcel(ProfileDetail profileDetail, List<File> fileList) {

      Boolean flag=false;

      //物料编码
      String matModel = profileDetail.getMatModel();
      /*//版本名称
      String matVerName = profileDetail.getMatVerName();*/
      //工序名称
      String proName = profileDetail.getProName();
      //文件路径
      String content = profileDetail.getContent();
      if(StringUtils.isBlank(matModel)  && StringUtils.isBlank(proName) && StringUtils.isBlank(content)){
          return "该行数据为空/删除不彻底";
      }
      Map<String, Mat> matMap = uploadMapper.queryMat();


      if(StringUtils.isBlank(matModel)){
          return "产品编码为空";
      }
      if(!matMap.containsKey(matModel)){
          return "产品编码填写错误/不规范";
      }
      Integer matId = matMap.get(matModel).getMatId();
      profileDetail.setMatId(matId);
      /*if(StringUtils.isBlank(matVerName)){
          return "产品型号为空";
      }
      Map<String, MatVer> matVerMap = uploadMapper.queryMatVerByMatId(matMap.get(matModel).getMatId());
      if(!matVerMap.containsKey(matVerName)){
          return "产品型号填写错误/不规范";
      }
      profileDetail.setMatVerId(matVerMap.get(matVerName).getMatVerId());
      if(!matVerMap.containsKey(matVerName)){
          return "产品型号填写错误/不规范";
      }*/
      Map<String, Pro> proMap = uploadMapper.queryPro();
      if(!StringUtils.isEmpty(proName)){
          if(!proMap.containsKey(proName)){
              return "工序名称填写错误/不规范";
          }
      }
      /*Map<String, MatFlow> matFlowMap = uploadMapper.queryMatFlowByMatVerId(matVerMap.get(matVerName).getMatVerId());
      if(!StringUtils.isBlank(proName) && ! matFlowMap.containsKey(proName)){
          return "工序名称填写错误/不规范";
      }*/
      if(!StringUtils.isBlank(proName) && proMap.containsKey(proName)){
          profileDetail.setProId(proMap.get(proName).getProId());
          //判断指定产品下的版本是否存在工序
          List<ProfileDetail> profileDetailList = selectProFileList(profileDetail);
          for (ProfileDetail detail : profileDetailList) {
              if(profileDetail.getProName().equals(detail.getProName())){
                  return "指定产品下已存在导入工序";
              }

          }
          if(!StringUtils.isEmpty(content)){
              for (File file : fileList) {
                  //找到模板中填入的文件名称并进行格式转换
                  if(content.equals(file.getName())){
                      //对文件进行处理
                      JSONObject jsonObjectResult = FileUtil.fileProcess(file);
                      if(jsonObjectResult.getStatus().equals(JSONObject.ERROR)){
                          return jsonObjectResult.getMsg();
                      }else{
                          profileDetail.setRelUrl(jsonObjectResult.getMsg());
                      }
                      flag=true;
                      break;
                  }
              }
              if(!flag){
                  return "文件名填写错误/不存在";
              }
          }

      }
      return null;
  }

    @Override
    @Transactional
    public JSONObject updateListProfileDetail(List<ProfileDetail> profileDetailList) {
        if(!profileDetailList.isEmpty()){
            for (ProfileDetail profileDetail : profileDetailList) {
                /*
                  根据产品和编码查询，工艺文件库中是否已经存在
                */
                List<Profile> profiles = uploadMapper.checkHasProfile(profileDetail.getMatId());
                if(profiles.isEmpty()){
                    profileDetail.setUuid(UuidUtil.getUuid());
                    uploadMapper.insertProfile(profileDetail);
                }else{
                    profileDetail.setUuid(UuidUtil.getUuid());
                    profileDetail.setProfileId(profiles.get(0).getProfileId());
                    uploadMapper.updateProfile(profileDetail);
                }
            }
            int updateNum = uploadMapper.updateListProfileDetail(profileDetailList);
            if(updateNum>0){
                return JSONObject.build(200,"新增成功",updateNum);
            }else{
                return JSONObject.build(500,"新增失败",updateNum);
            }
        }
        return JSONObject.build(200,"新增成功",null);
    }
}
